package com.wh.service.impl;

import com.wh.dao.BaseDao;
import com.wh.dao.ext.SysDeptDao;
import com.wh.model.BaseModel;
import com.wh.model.SysDept;
import com.wh.service.IExt.ISysDeptService;
import com.legend.utils.*;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
*author:徐洪昌
*blog:http://cnblogs.com/xusir
*datetime:2014-02-12 21:41:47
*/
public class SysDeptService extends BaseServiceImp implements ISysDeptService {

    Logger log = Logger.getRootLogger();

    private SysDeptService() {
    }

    //volatile修饰的写变量不能和之前的读写代码调整，读变量不能和之后的读写代码调整
    private volatile static ISysDeptService instance = null;

    /**
     * 单例
     *
     * @return
     */
    public static ISysDeptService getInstance() {
        if (instance == null) {
            synchronized (ISysDeptService.class) {
                if (instance == null) {
                    instance = new SysDeptService();
                }
            }
        }
        return instance;
    }

    /**
     * 批量删除数据
     */
    public ReturnResult delByIds(String ids) {
        SqlSession session = getSqlSession();
        ReturnResult result = new ReturnResult();
        try {
            SysDeptDao dao = session.getMapper(SysDeptDao.class);
            result = delByMap(dao, ids);
        } catch (Exception ex) {
            log.error(ex.getMessage());
        } finally {
            session.close();
        }
        return result;
    }

    /**
     * 分页数据查询
     *
     * @param pageIndex
     * @param pageSize
     * @param map
     * @return
     */
    public ReturnResult selectByMap(int pageIndex, int pageSize, Map<String, Object> map) {
        SqlSession session = getSqlSession();
        SysDeptDao dao = session.getMapper(SysDeptDao.class);
        int recordNum = (pageIndex - 1) * pageSize;
        List<SysDept> lst = session.selectList(ConfigUtils.getInstance().getDaoPackage(".SysDeptDao.selectByMap"), map, new RowBounds(recordNum, pageSize));
        int total = dao.count(map);
        ReturnResult result = new ReturnResult();
        result.setMsg(i18nUtils.getVal("100001000"));
        result.setRows(lst);
        result.setTotal(total);
        result.setStatus(Status.SUCCESS);
        session.close();
        return result;
    }

    @Override
    public ReturnResult insertByModel(BaseModel model) {
        SqlSession session = getSqlSession();
        SysDeptDao dao = session.getMapper(SysDeptDao.class);
        ReturnResult result = insertByModel(dao, model);
        session.close();
        return result;
    }

    @Override
    public ReturnResult editByModel(BaseModel model) {
        SqlSession session = getSqlSession();
        SysDeptDao dao = session.getMapper(SysDeptDao.class);
        ReturnResult result = editByModel(dao, model);
        session.close();
        return result;
    }

    /**
     * 根据ID获取一条数据
     *
     * @param id
     * @return
     */
    public ReturnResult selectById(String id) {
        SqlSession session = getSqlSession();
        ReturnResult result = new ReturnResult();
        result.setStatus(Status.ERROR);
        BaseDao dao = session.getMapper(SysDeptDao.class);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        List<SysDept> lst = dao.selectByMap(map);
        if (lst.size() == 1) {
            result.setData(lst.get(0));
            result.setStatus(Status.SUCCESS);
        } else {
            result.setMsg(i18nUtils.getVal("100004000"));
        }
        session.close();
        return result;
    }

    /**
     * 批量审核数据
     */
    public ReturnResult audit(int state, String[] idArr) {
        SqlSession session = getSqlSession();
        SysDeptDao dao = session.getMapper(SysDeptDao.class);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("idArr", idArr);
        map.put("state", state);
        Integer num = dao.audit(map);
        ReturnResult result = new ReturnResult();
        if (num > 0) {
            result.setMsg(i18nUtils.getVal("100001000"));
            result.setStatus(Status.SUCCESS);
        }
        session.close();
        return result;
    }

    /**
     * 排序
     *
     * @param sorts
     * @param ids
     * @return
     */
    public ReturnResult sort(String sorts, String ids) {
        ReturnResult result;
        SqlSession session = getSqlSession();
        SysDeptDao dao = session.getMapper(SysDeptDao.class);
        result = sort(session, dao, sorts, ids);
        if (session != null)
            session.close();
        return result;
    }

    /**
     * 获得重组后的 部门列表
     */
    public List<SysDept> getDeptList(boolean defaultOption) {
        SqlSession session = getSqlSession();
        SysDeptDao dao = session.getMapper(SysDeptDao.class);
        List<SysDept> lst = dao.selectByMap(null);
        List<SysDept> lstAlias = getDeptList(lst, "0");
        if (defaultOption) {
            SysDept alias = new SysDept();
            alias.setId("0");
            alias.setDeptName("顶级节点");
            lstAlias.add(0, alias);
        }
        session.close();
        return lstAlias;
    }

    /**
     * 重组部门列表为树形结构
     *
     * @param lst 原列表
     * @param pid 上级ID
     * @return 重组后的树
     */
    private List<SysDept> getDeptList(List<SysDept> lst, String pid) {
        List<SysDept> tmpLst = new ArrayList<SysDept>();
        for (SysDept dept : lst) {
            if (dept.getDeptPid().equals(pid)) {
                SysDept deptAlias = new SysDept();
                deptAlias.setId(dept.getId());
                deptAlias.setDeptName(dept.getDeptName());
                deptAlias.setDeptMemo(dept.getDeptMemo());
                deptAlias.setSort(dept.getSort());
                deptAlias.setStatus(dept.getStatus());
                deptAlias.setCreateDate(dept.getCreateDate());
                deptAlias.setUpdateDate(dept.getUpdateDate());
                deptAlias.setFoundDate(dept.getFoundDate());
                deptAlias.setChildren(getDeptList(lst, dept.getId()));
                tmpLst.add(deptAlias);
            }
        }
        return tmpLst;
    }
}

